package com.nju.bean.image;

import java.io.IOException;

import com.bean.newsBang.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

public class ImageViewMaker {
	public static View makeEditor(Activity activity){
		return makeView(activity,R.layout.photo_edit);
	}

	public static View makeSharerr(Activity activity){
		return makeView(activity,R.layout.photo_view);
	}
	
	private static View makeView(final Activity activity,int id){
    	LayoutInflater inflater = activity.getLayoutInflater();    	
    	View imageEditor = inflater.inflate(id,null);
    	
    	final ImageView photoView= (ImageView) imageEditor.findViewById(R.id.image);
    	
    	ImageButton rotate90 = (ImageButton) imageEditor.findViewById(R.id.rotate_redo);
    	ImageButton rotate270 = (ImageButton) imageEditor.findViewById(R.id.rotate_undo);
    	ImageButton share = (ImageButton) imageEditor.findViewById(R.id.share);
    	rotate90.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ImageHandler ih = new ImageHandler(photoView.getDrawable());
				Bitmap b = ih.rotate90();
				photoView.setImageBitmap(b);
			}});
    	
    	rotate270.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ImageHandler ih = new ImageHandler(photoView.getDrawable());
				Bitmap b = ih.rotate270();
				photoView.setImageBitmap(b);
			}});
    		if(share!=null)
	    		share.setOnClickListener(new OnClickListener(){
	
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					ImageHandler ih = new ImageHandler(photoView.getDrawable());
					try {
						ih.shareImage(activity);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}});
    	
		return imageEditor;
	}
	
}